// pages/materialData/materialData.js
const { saveMaterialList, getMaterialList, deleteAliyunfileList, deleteMaterialList } = require('../../api/geData.js')
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    materialList:[],
    enclosureList:[],
    // wordname:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMasList()
  },
  // 消息列表切换
  switchmaterialTab(e) {
    // console.log(e);
    let that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
      })
    }
  },
  // 切换数据
  swiperChange(e) {
    this.setData({
      currentTab: e.detail.current,
    })
    if(this.data.currentTab=='0'){
      this.getMasList()
    } else if (this.data.currentTab == '1')
    {
      getMaterialList(wx.getStorageSync('userInfo').cluesOrStuId).then(res => {
        let enclosureLists = res.list,
             matList = [];
        enclosureLists.forEach(item => {
          if (item.type == '1') {
            matList.push(item)
          }
        })
        this.setData({
          enclosureList: matList
        })
      })
    }
  },
  // 提交素材
  submitMaterialList(){
    let that = this
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      success(res) {
        let tempFilePaths = res.tempFiles[0]
        // console.log(tempFilePaths)
        let fileType = tempFilePaths.path.split('.')
        if (fileType[1] != 'doc' && fileType[1] != 'docx' && fileType[1] != 'pdf' && fileType[1] != 'zip' && fileType[1] != 'rar') {
          wx.showModal({
            content: '选择的文件无效，请重新选择',
            confirmColor: '#2A8EFF',
            showCancel: false,
            confirmText: '确定'
          })
        } else if (tempFilePaths.size > (8 * 1024 * 1024)){
          wx.showModal({
            content: '文件大小超过8M，请检查文件大小',
            confirmColor: '#2A8EFF',
            showCancel: false,
            confirmText: '确定'
          })
        }
        else {
          wx.uploadFile({
            url: app.globalData.baseUrl + '/file/upload',
            filePath: tempFilePaths.path,
            name: 'file',
            formData:{
              wordname: tempFilePaths.name
            },
            success: function (res) {
              var data = JSON.parse(res.data)
          if (data.state=='SUCCESS')
              {
            saveMaterialList(wx.getStorageSync('userInfo').cluesOrStuId, data.url, data.key, data.size, data.title,0).then(res=>{
                  that.getMasList()
                })
              }
            }
          })
        }
        }
        })
      },
      //获取素材列表
      getMasList(){
        getMaterialList(wx.getStorageSync('userInfo').cluesOrStuId).then(res=>{
            let materialLists=res.list,
                matList = [];
            materialLists.forEach(item => {
              if (item.type == '0') {
                matList.push(item)
              }
            })
            this.setData({
              materialList: matList
            })
        })
      },
      //删除素材
  deleteSourceList(e) {
    let ids=e.currentTarget.dataset.ids;
    let keys = e.currentTarget.dataset.keys;
    let that=this
    wx.showModal({
      content: '删除后，讲师将无法看到您提交的就业素材。确定删除该附件？',
      cancelText: '取消',
      confirmText: '确定',
      confirmColor: '#2A8EFF',
      success(res) {
        if (res.confirm) {
          deleteAliyunfileList(ids, keys).then(res => {
            if (res.status == "success") {
              deleteMaterialList(res.id).then(data => {
                that.getMasList()
              })
            }
          })
        }}
    })
  },
    // 查看素材
    downSourceList(e){
      let ids = e.currentTarget.dataset.ids;
      let downurl = e.currentTarget.dataset.downurl
      wx.downloadFile({
        url: downurl,
        success(res) {
          if (res.statusCode === 200) {
            let tempFilePath = res.tempFilePath
            // console.log(tempFilePath)
            wx.showLoading({
              title: '加载中',
              mask: true
            })
            wx.openDocument({
              filePath: tempFilePath,
              success: function (res) {
                // console.log(res)
              },fail(res){
                wx.showLoading({
                  title: '加载中',
                  mask: true
                })
                wx.showModal({
                  content: '小程序暂不支持查看该文件类型，请登录国科学院网页端进行查看下载',
                  confirmText: '我知道了',
                })
                wx.hideLoading()
              },complete(res){
                wx.hideLoading()
              }
            })
          }
        }
      })
    },
    // 查看附件
  downEnclosureList(e) {
    let ids = e.currentTarget.dataset.ids;
    let downurl = e.currentTarget.dataset.downurl
    wx.downloadFile({
      url: downurl,
      success(res) {
        if (res.statusCode === 200) {
          let tempFilePath = res.tempFilePath
          // console.log(tempFilePath)
          wx.openDocument({
            filePath: tempFilePath,
            success: function (res) {
              // console.log(res)
            }, fail(res) {
              wx.showModal({
                content: '该文件不支持预览，请登录国科学院官网进行查看下载',
                confirmText: '我知道了',
              })
            }, complete(res) {
              wx.hideLoading()
            }
          })
        }
      }
    })
  }
    })
